﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

namespace MyWebPagesStarterKit.Controls
{
    /// <summary>
    /// Summary description for TemplatedRegion
    /// </summary>
    [ToolboxData("<{0}:TemplatedRegion runat=server></{0}:TemplatedRegion>"), DisplayName("TemplatedRegion")]
    public class TemplatedRegion : WebControl, INamingContainer
    {
        private Position _position;

        public TemplatedRegion() { }

        /*
        protected override void EnsureChildControls()
        {
            if (!ChildControlsCreated)
            {
                CreateChildControls();
                ChildControlsCreated = true;
            }
        }*/

        protected override void CreateChildControls()
        {
            PlaceHolder ph = new PlaceHolder();
            WebPage _page = WebPage.Current;
            WebSite _website = WebSite.GetInstance();
            
            ph.Controls.Clear();
            foreach (ISection section in _page.Sections)
            {
                if (section.Position == _position)
                {
                    SectionControlBaseClass ctl = (SectionControlBaseClass)Page.LoadControl(section.UserControl);

                    if (HttpContext.Current.User.Identity.IsAuthenticated && PowerUserExt.HasRights(Page.User, _page, section, Rule.EDIT))
                    {
                        UserControl admin = (UserControl)Page.LoadControl("~/SectionControls/SectionAdmin.ascx");
                        ((ISectionAdmin)admin).SectionControl = ctl;
                        ((ISectionAdmin)admin).WebPage = _page;
                        ph.Controls.Add(admin);
                    }

                    if (GetAccess(_page, section))
                        ph.Controls.Add(ctl);

                    ctl.Section = section;
                }
            }

            this.Controls.Add(ph);
            ChildControlsCreated = true;
        }



        private bool GetAccess(WebPage page, ISection section)
        {   
            bool access = false;

            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (!section.IsOnline && PowerUserExt.HasRights(Page.User, page, section, Rule.EDIT))
                    access = PowerUserExt.HasRights(Page.User, page, section, Rule.EDIT);
                else
                    access = PowerUserExt.HasRights(Page.User, page, section, Rule.VIEW);
            }
            else
            {
                if (section.IsOnline)
                    access = PowerUserExt.HasRights(Page.User, page, section, Rule.VIEW);
                else
                    access = false;
            }
            return access;
        }



        /// <summary>
        /// Position (Region) of this section
        /// </summary>
        [Browsable(true), PersistenceMode(PersistenceMode.Attribute)]
        public Position Position
        {
            get
            {
                if (_position == Position.UNKNOWN)
                    throw new InvalidEnumArgumentException("TemplatedRegion, Position is missing.");
                return _position;
            }
            set { this._position = value; }
        }
    }
}
